import { defineStore } from 'pinia'
import { asyncRoutes, constantRoutes, routerArray, notFoundRouter } from '@/routers/index'
import { hasPermission, filterAsyncRoutes } from "@/utils/routers"
import { filterKeepAlive, filterRoutes } from "@/utils/routers";
import { adminmenu } from '@/api/admin'
import Layout from '@/layout/index.vue'
export const usePermissionStore = defineStore({
    // id: 必须的，在所有 Store 中唯一
    id: 'permissionState',
    // state: 返回对象的函数
    state: () => ({
        // 路由
        routes: [],
        // 动态路由
        addRoutes: [],
        // 缓存路由
        cacheRoutes: {},
    }),
    getters: {
        permission_routes: state => {
            return state.routes
        },
        keepAliveRoutes: state => {
            return filterKeepAlive(asyncRoutes)
        }
    },
    // 可以同步 也可以异步
    actions: {
        // 生成路由
        generateRoutes(roles) {
            return new Promise(resolve => {
                let accessedRoutes = []
                //接口
                adminmenu({}).then(res => {
                    if (res.code == 200) {
                        let list = res.data
                        list.forEach(item => {
                            let local = asyncRoutes.find(row => {
                                return item.href == row.path
                            })
                            if (local) {
                                let obj = {
                                    path: item.href,
                                    name: local.name,
                                    redirect: local.redirect,
                                    component: Layout,
                                    meta: { title: item.title, icon: item.icon },
                                    children: []
                                }
                                item.children.forEach(it => {
                                    let localChild = local.children.find(row => {
                                        return it.href == row.path
                                    })
                                    if (localChild) {
                                        let child = {
                                            path: it.href,
                                            name: localChild.name,
                                            component: localChild.component,
                                            meta: { title: it.title, icon: it.icon },
                                            hidden:it.hidden
                                        }
                                        obj.children.push(child)
                                    }
                                })
                                accessedRoutes.push(obj)
                            }
                        })
                    }
                    this.routes = constantRoutes.concat(accessedRoutes)
                    this.addRoutes = accessedRoutes
                    resolve(accessedRoutes)
                })
                // 本地
                // if (roles && roles.length && !roles.includes('admin')) {
                //     accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
                // } else {
                //     accessedRoutes = asyncRoutes || []
                // }
                // accessedRoutes = accessedRoutes.concat(notFoundRouter)
                // this.routes = constantRoutes.concat(accessedRoutes)
                // this.addRoutes = accessedRoutes
                // resolve(accessedRoutes)
            })
        },
        // 清楚路由
        clearRoutes() {
            console.log('clearRoutes')
            this.routes = []
            this.addRoutes = []
            this.cacheRoutes = []
        },
        getCacheRoutes() {
            this.cacheRoutes = filterKeepAlive(asyncRoutes)
            return this.cacheRoutes
        }
    },

})



